package com.inspinia.upms.server.controller.upms;

import com.inspinia.base.common.model.AjaxResult;
import com.inspinia.base.pagination.Pager;
import com.inspinia.upms.common.model.dept.UpmsDept;
import com.inspinia.upms.common.model.dept.UpmsDeptType;
import com.inspinia.upms.common.model.dept.UpmsDeptVo;
import com.inspinia.upms.rpc.api.UpmsDeptService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.beans.PropertyEditorSupport;
import java.lang.reflect.InvocationTargetException;

/**
 * 部门信息
 * 
 * @author ruoyi
 */
@Controller
@RequestMapping("upms/dept")
public class UpmsDeptController {
    private String prefix = "upms/department/";

    @Resource
    private UpmsDeptService upmsDeptService;

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(UpmsDeptType.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) throws IllegalArgumentException {
                UpmsDeptType departmentType = UpmsDeptType.valueOf(Integer.valueOf(text));
                setValue(departmentType);
            }
        });
    }

    /**
     * 跳转分页页面
     *
     * @return
     */
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public String toPaginationPage() {
        return prefix + "department-pagination";
    }

    /**
     * 分页查询
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "list", method = RequestMethod.POST)
    public AjaxResult paging(UpmsDeptVo departmentVo) throws InvocationTargetException, IllegalAccessException {
        Pager<UpmsDeptVo> page = upmsDeptService.findPage(departmentVo);
        return new AjaxResult(true,page);
    }

    /**
     * 跳转添加页面
     *
     * @return
     */
    @RequestMapping(value = "create", method = RequestMethod.GET)
    public String goCreate(Long orgId,Model model){
        model.addAttribute("departmentType",UpmsDeptType.values());
        return prefix + "/department-create";
    }

    /**
     * 跳转添加页面
     *
     * @return
     */
    @RequestMapping(value = "edit", method = RequestMethod.GET)
    public String goEdit(Model model) {
        model.addAttribute("departmentType",UpmsDeptType.values());
        return prefix + "/department-edit";
    }
    /**
     * 编辑Department
     *
     * @param department
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "edit", method = RequestMethod.POST)
    public AjaxResult edit(@Valid UpmsDept department) {
        upmsDeptService.update(department);
        return new AjaxResult(true);
    }

    /**
     * 创建Department
     *
     * @param department
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "create", method = RequestMethod.POST)
    public AjaxResult create(@Valid UpmsDept department) {
        upmsDeptService.insert(department);
        return new AjaxResult(true);
    }

    /**
     * 根据id获取Department
     *
     * @param id id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "get", method = RequestMethod.GET)
    public AjaxResult get(long id) {
        UpmsDept bean = upmsDeptService.get(id);
        return new AjaxResult(true, bean);
    }

    /**
     * 根据id获取部门vo,Vo相对entity数据要多一些
     *
     * @param id id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getVo", method = RequestMethod.GET)
    public AjaxResult getVo(long id) throws InvocationTargetException, IllegalAccessException {
        UpmsDeptVo bean = upmsDeptService.getVo(id);
        return new AjaxResult(true, bean);
    }

    /**
     * 删除Department
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public AjaxResult delete(Long id) {
        int delete = upmsDeptService.delete(id);
        if (delete > 0) {
            return new AjaxResult(true);
        }
        return new AjaxResult(false, "删除失败");
    }

    /**
     * 批量删除部门
     *
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "batchDelete", method = RequestMethod.POST)
    public AjaxResult delete(@RequestParam(name = "ids[]", required = false, defaultValue = "") Long[] ids) {
        int delete = upmsDeptService.batchDelete(ids);
        if (delete == ids.length) {
            return new AjaxResult(true);
        }
        return new AjaxResult(false, "有" + (ids.length - delete) + "个部门删除失败!");
    }
}
